package Client;

import Entity.TransactionPojo;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.bson.types.ObjectId;
 
public class ClienteRest { 
	
	public static void main(String[] args) {
            send();
	}
        
        public static void send(){
            for (int i = 1; i<2000;i++){
            TransactionPojo transaction = new TransactionPojo();
            
            
                transaction.setFileId(new ObjectId().toString());
                transaction.setFileNumber(2);
                transaction.setName("Oscar Andres Dussan Garcia");
                transaction.setNin("1110470707");
                transaction.setNinType(1);
                transaction.setCardNumber("6016607113769849");
                transaction.setCardType(1);
                transaction.setCardCode(1243);
                transaction.setCardExpirationDate(new Date());
                transaction.setBank(new ObjectId().toString());
                transaction.setFranchise(new ObjectId().toString());                   
                transaction.setPurchaseDate(new Date().getTime());
                transaction.setPurchaseAmount(350000);
                transaction.setPurchaseDetail("Compra de un articulo");
            
	  try { 
		URL url = new URL("http://localhost:8080/WSQueueRest/resources/transaction/post");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/json");
                                
                Gson transactionJson = new Gson();                
		String input = transactionJson.toJson(transaction);
//                        
//                        ""
//                        + "{\"cardNumber\":\""+transaction.getCardNumber()+"\","
//                        + "\"cardType\":"+transaction.getCardType()+","
//                        + "\"fileId\":\""+transaction.getFileId()+"\","
//                        + "\"franchise\":\""+transaction.getFranchise()+"\","
//                        + "\"name\":\""+transaction.getName()+"\","
//                        + "\"nin\":\""+transaction.getNin()+"\","
//                        + "\"ninType\":"+transaction.getNinType()+","
//                        + "\"purchaseAmount\":"+transaction.getPurchaseAmount()+","
//                        + "\"purchaseDate\":\""+transaction.getPurchaseDate()+"\","
//                        + "\"purchaseDetail\":\""+transaction.getPurchaseDetail()+"\""
//                        + "}";
                //for(int i = 0;i<100;i++){
                
                
                transaction.setFileId(new ObjectId().toString());
                transaction.setFileNumber(2);
                transaction.setName("Oscar Andres Dussan Garcia");
                transaction.setNin("1110470708");
                transaction.setNinType(1);
                transaction.setCardNumber("6016607113769849");
                transaction.setCardType(1);
                transaction.setCardCode(1243);
                transaction.setCardExpirationDate(new Date());
                transaction.setBank(new ObjectId().toString());
                transaction.setFranchise(new ObjectId().toString());                   
                transaction.setPurchaseDate(new Date().getTime());
                transaction.setPurchaseAmount(350000);
                transaction.setPurchaseDetail("Compra de un articulo");
                
                
		OutputStream os = conn.getOutputStream();
		os.write(input.getBytes());
		os.flush();
          //}
		if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
			throw new RuntimeException("HTTP: code : "
				+ conn.getResponseCode());
		}
 
		BufferedReader br = new BufferedReader(new InputStreamReader(
				(conn.getInputStream())));
 
		String outransactionut;
		System.out.println(i+".)Received from server : OK\n");
		while ((outransactionut = br.readLine()) != null) {
			System.out.println(outransactionut);
		}
 
		conn.disconnect();
 
	  } catch (MalformedURLException e) {
                System.out.println("Client ERROR - MalformedURL Exception: "); 
                e.printStackTrace(); 
	  } catch (IOException e) {
                System.out.println("Client ERROR - IOEXCEPTION: "); 
		e.printStackTrace();
 
	 }
        }}
 
}